package com.wish.contract.controller;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.common.core.domain.R;
import com.wish.common.core.utils.poi.ExcelUtil;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.common.security.utils.SecurityUtils;
import com.wish.contract.po.Contract;
import com.wish.contract.service.IContractService;
import com.wish.contract.vo.QueryVo;
import com.wish.system.api.RemoteHourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Andy
 * @since 2022-11-19 09:24:23
 */
@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController {
    @Autowired
    IContractService contractService;
    @Autowired
    RemoteHourseService remoteHourseService;
    /**
     * 笔者      Andy
     * 描述:    queryContract 合同列表 分页
     * 时间:    2022-11-21 16:24
     * 参数:    [vo]
     * 返回值:   com.wish.common.core.web.page.TableDataInfo
     */
    @RequestMapping("queryContract")
    @Log(title = "合同列表 分页", businessType = BusinessType.OTHER)
    @RequiresPermissions("contract:contract:queryContract")
    public TableDataInfo queryContract(QueryVo vo) {
        startPage();
        QueryWrapper<Contract> wrapper = new QueryWrapper<>();
        //合同模糊
        if (vo.getContract_no()!=null) {
            wrapper.like("contract_no", vo.getContract_no()).or().like("last_contract_id", vo.getContract_no());
        }
        //租客信息模糊
        if (vo.getSignUserMessage()!=null) {
            wrapper.like("sign_user_mobile", vo.getSignUserMessage()).or().like("sign_user_emergency_phone", vo.getSignUserMessage()).or().eq("sign_user_card", vo.getSignUserMessage()).or().eq("sign_user_name",vo.getSignUserMessage());
        }
        //房东信息模糊
        if (vo.getOwnerMessage()!=null) {
            wrapper.eq("owner_name", vo.getOwnerMessage()).or().like("owner_mobile", vo.getOwnerMessage()).or().eq("owner_id_card",vo.getOwnerMessage());
        }
        //时间查询
        if (vo.getLeaseStartTime() != null || vo.getLeaseEndTime() != null || vo.getConSignTime() != null) {
            wrapper.gt("lease_start_time", vo.getLeaseStartTime()).or().le("lease_end_time", vo.getLeaseEndTime()).eq("con_sign_time", vo.getConSignTime());
        }
        //资金查询
        if (vo.getMonthRent() != null || vo.getDepositMoney() != null) {
            wrapper.eq("month_rent", vo.getMonthRent()).or().eq("deposit_money", vo.getDepositMoney());
        }
        List<Contract> list = contractService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 笔者      Andy
     * 描述:    export  合同导出export
     * 时间:    2022-11-21 16:25
     * 参数:    [response, tbContract]
     * 返回值:   void
     */
    @RequiresPermissions("contract:contract:export")
    @Log(title = "excel导出合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        List<Contract> list = contractService.list();
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "excel导出合同数据");
    }

    /**
     * 笔者      Andy
     * 描述:    getInfo 根据id查询单条记录
     * 时间:    2022-11-21 16:25
     * 参数:    [id]
     * 返回值:   com.wish.common.core.web.domain.AjaxResult
     */
    @RequiresPermissions("contract:contract:query")
    @GetMapping(value = "/{id}")
    @Log(title = "根据id查询单条", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(contractService.getById(id));
    }

    /**
     * 笔者      Andy
     * 描述:    add 添加合同
     * 时间:    2022-11-21 16:26
     * 参数:    [tbContract]
     * 返回值:   com.wish.common.core.web.domain.AjaxResult
     */
    @RequiresPermissions("contract:contract:add")
    @Log(title = "添加合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Contract tbContract) {
        Snowflake snowflake = new Snowflake();
        tbContract.setContractNo("zf"+snowflake.nextIdStr());
        tbContract.setHouseId(UUID.fastUUID().variant());
        tbContract.setConSignTime(new Date());
        tbContract.setCreateTime(new Date());
        tbContract.setCreateUserid(SecurityUtils.getUserId());
        tbContract.setOperId(Math.toIntExact(SecurityUtils.getUserId()));
        return toAjax(contractService.save(tbContract));
    }

    /**
     * 笔者      Andy
     * 描述:    edit 修改合同信息
     * 时间:    2022-11-21 16:26
     * 参数:    [tbContract]
     * 返回值:   com.wish.common.core.web.domain.AjaxResult
     */
    @RequiresPermissions("contract:contract:edit")
    @Log(title = "修改合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contract tbContract) {
        return toAjax(contractService.saveOrUpdate(tbContract));
    }

    /**
     * 笔者      Andy
     * 描述:    remove 单删或批删
     * 时间:    2022-11-21 16:27
     * 参数:    [ids]
     * 返回值:   com.wish.common.core.web.domain.AjaxResult
     */
    @RequiresPermissions("contract:contract:remove")
    @Log(title = "单删或批删", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        ArrayList<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        return toAjax(contractService.removeByIds(list));
    }
    /**
     * 笔者      Andy
     * 描述:    queryHouse 远程调用房源
     * 时间:    2022-11-22 15:39
     * 参数:    []
     * 返回值:   com.wish.common.core.web.page.TableDataInfo
     */
    @RequiresPermissions("contract:contract:queryHouse")
    @Log(title = "远程调用房源", businessType = BusinessType.OTHER)
    @GetMapping("/queryHouse")
    public TableDataInfo queryHouse() {
        startPage();
        return getDataTable(remoteHourseService.list().getRows());
    }
    @GetMapping("/exportp/{id}")
    public R exportp(Integer id){
        return R.ok(0,contractService.getById(id).toString());
    }
}

